#include "LotteryRoom.hh"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "IO.hh"

LotteryRoom::LotteryRoom(const std::string& name, const std::string& description)
    : Room(name, description), drawCount(0), gotArtifact30(false), gotArtifact60(false) {
    std::srand(static_cast<unsigned int>(std::time(nullptr))); // 确保随机种子 Make sure the random seed
}

void LotteryRoom::enterRoom(Player& player) {
    const auto& allItems = getAllShopItems();
    std::cout << "\n[You have entered the Lottery Room. Each draw costs 5 gold.]\n";
    std::cout << "[1%] chance to draw a legendary artifact. Guaranteed artifacts at 30 and 60 draws!]\n";

    while (true) {
        std::cout << "\nYour Gold: " << player.getGold() << "\n";
        std::cout << "Draw Count: " << drawCount << "\n";
        std::cout << "1. Draw (5 gold)\n";
        std::cout << "2. Leave\n";
        std::cout << "Choice: ";
        int choice=IO::getNumber();
        
        
        if (choice == 2) break;
        if(choice!=1) {
            cout<<"invalid input.\n";
            continue;
        }
        if (player.getGold() < 5) {
            std::cout << "Not enough gold to draw.\n";
            continue;
        }

        player.spendGold(5);
        drawCount++;

        // 保底机制
        if (drawCount == 30 && !gotArtifact30) {
            gotArtifact30 = true;
            std::cout << " [Pity Draw] You received the artifact: Spear[Artifact]!\n";
            player.equipItem({"Spear[Artifact]", "Destroy everything.", Item::Type::Weapon, 50, 5});
            continue;
        }
        if (drawCount == 60 && !gotArtifact60) {
            gotArtifact60 = true;
            std::cout << " [Pity Draw] You received the supreme artifact: Shield[Artifact]!\n";
            player.equipItem({"Shield[Artifact]", "Resist everything.", Item::Type::Armor, 50, 5});
            continue;
        }

        // 概率抽奖
        int roll = std::rand() % 100; // 0-99

        if (roll < 1) {
            std::cout << "You won a rare artifact: Spear[Artifact]!\n";
            player.equipItem({"Spear[Artifact]", "Destroy everything.", Item::Type::Weapon, 50, 5});
            gotArtifact30 = true;
        } else if (roll < 2){
            std::cout << "You won a rare artifact: Shield[Artifact]!\n";
            player.equipItem({"Shield[Artifact]", "Resist everything.", Item::Type::Armor, 50, 5});
            gotArtifact60 = true;
        }
        else if (roll < 25) {
            std::cout << "You gained 10 gold!\n";
            player.addGold(10);
        } else if (roll < 50) {
            const auto& allItems = getAllShopItems();
            std::vector<Item> potions;
            for (const auto& item : allItems) {
                if (item.type == Item::Type::HealPotion ||
                    item.type == Item::Type::BoostPotion ||
                    item.type == Item::Type::DefencePotion) {
                    potions.push_back(item);
                }
            }
            if (!potions.empty()) {
                int index = std::rand() % potions.size();
                const Item& potion = potions[index];
                std::cout << " You received a potion: " << potion.name << "!\n";
                player.addItem(potion);
            } else {
                std::cout << "Hmm... No potions found.\n";
            }
        } else if (roll < 75) {
            std::cout << "A small blessing... you recover 20 HP.\n";
            if (player.getHealth() <= player.getMaxHealth() - 20){
                player.setHealth(player.getHealth() + 20);
            }else{
                player.setHealth(player.getMaxHealth());
            }
        } else {
            std::cout << "You received: Author's blessing!\n";
        }
    }
}
